//
//  VideoController.m
//  播放器模拟(列表)
//
//  Created by lanou3g on 15/11/2.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "VideoController.h"
#import "VideoView.h"
#import "SliderToolbar.h"
#import "VideoModel.h"
#import "MBProgressHUD+MJ.h"

@interface VideoController () <VideoViewDelegate, SliderDelegate, SliderToolbarDelegate>



@property (nonatomic, strong) VideoView *videoView;
@property (nonatomic, strong) SliderToolbar *sliderView;
@property (nonatomic, assign) BOOL checkPlaying;
@property (nonatomic, assign) CGFloat lastPlayPoint;
@property (nonatomic, strong) UIImageView *backGroundView;

@end


@implementation VideoController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.hidesBottomBarWhenPushed = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setBackgroundColor:[UIColor orangeColor]];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.hidesBottomBarWhenPushed = YES;
    
    self.backGroundView.alpha = 0.2;
    
    
    [MBProgressHUD showMessage:@"加载中.."];
    
    self.videoView.frame = CGRectMake(5, 50, self.view.bounds.size.width - 20, 300);
    self.videoView.delegate = self;

    
    // 设置工具条的位置
    self.sliderView.frame = CGRectMake(10, 400, self.view.bounds.size.width - 20, 50);
    self.sliderView.layer.cornerRadius = 10;
    self.sliderView.layer.masksToBounds = YES;
    self.sliderView.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.3];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(back)];
    
    
    [self initialThumbView];
}



- (void)viewDidDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self.videoView.player pause];
    self.videoView.delegate = nil;
//    [self.videoView.player setRate:0];
    [self.videoView.player removeTimeObserver:self.videoView.obj];
    [self.videoView.player.currentItem removeObserver:self.videoView forKeyPath:@"status"];
    [self.videoView.player.currentItem removeObserver:self.videoView forKeyPath:@"loadedTimeRanges"];
    self.videoView.playerItem = nil;
    self.sliderView.playButton.selected = false;
    self.sliderView.slider = nil;
    self.sliderView = nil;
    
    // 销毁资源
    [self.videoView.player replaceCurrentItemWithPlayerItem:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self.videoView];
    self.videoView.player = nil;
    self.videoView = nil;
    
    
}


- (void)setVideoModel:(VideoModel *)videoModel
{
        
    _videoModel = videoModel;

    
    self.videoView.contentUrl = videoModel.mp4_url;
    
    
}




#pragma mark - VideoViewDelegate
- (void)playEnd:(VideoView *)slVideoView
{

}

- (void)canStartPlaying:(VideoView *)slVideoView
{
    self.view.userInteractionEnabled = YES;
    _sliderView.totalTime = _videoView.totalTime;
    _sliderView.currentTime = 0;
}

- (void)networkNotBest:(VideoView *)slVideoView
{
    
}

- (void)dontPlayer:(VideoView *)slVideoView
{
    [MBProgressHUD hideHUD];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD showError:@"未知错误"];
    });
}

- (void)bufferTimeLength:(CGFloat)time
{
    if (_sliderView.totalTime == 0) return;
    
    // 缓冲进度
    self.sliderView.slider.bufferProgress = time / _sliderView.totalTime;
}

- (void)currentPlayerTimeLength:(CGFloat)time
{
    // 现在的播放的时间
    self.sliderView.currentTime = time;
    // 现在播放的进度
    self.sliderView.slider.valueProgress = time / _sliderView.totalTime;
    // 记录当前播放进度, 以便下一次进来使用
    self.lastPlayPoint = time / _sliderView.totalTime;
}

#pragma mark - SliderDelegate
- (void)progressSliderDidStart:(Slider *)progressSlider value:(float)vlaue
{
    if (_videoView.playState == VideoPlayerPlaybackStatePlaying)
    {
        _checkPlaying = YES;
        
        [_videoView.player pause];
        
    }else
    {
        _checkPlaying = NO;
        [_videoView.player pause];
    }
}

- (void)progressSliderDidMoveEnd:(Slider *)progressSlider value:(float)value
{
    CGFloat speedTime = value * _sliderView.totalTime;
    
    int scale = _videoView.timeScale;
    NSLog(@"---scale---%d", scale);
    
    CMTime cmtime = CMTimeMake(speedTime * scale, scale);
    
    [_videoView speed:cmtime];
    self.sliderView.playButton.selected = YES;
    [_videoView.player play];
}

#pragma mark - SliderToolbarDelegate
- (void)sliderToolbar:(SliderToolbar *)sliderToolbar playButtonDidClick:(UIButton *)button
{
    if (button.selected == false) {
        [self.videoView.player pause];
    } else {
        [self.videoView.player play];
    }
}

- (void)initialThumbView
{
    self.sliderView.slider.thumbView.frame = CGRectMake(0, 0, 20, 20);
    self.sliderView.slider.thumbView.layer.cornerRadius = 10;
    self.sliderView.slider.thumbView.layer.masksToBounds = YES;
    self.sliderView.slider.thumbView.backgroundColor = [UIColor whiteColor];
    [self.sliderView.slider insertSubview:self.sliderView.slider.thumbView aboveSubview:self.sliderView.slider.bufferView];
}

- (void)back
{
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}
//- (BOOL)shouldAutorotate {
//    
//    return YES;
//    
//}

//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    
//    return UIInterfaceOrientationMaskAll;//支持4各方向的旋转。
//    
//}

- (void)didReceiveMemoryWarning
{
    
}


#pragma mark - 懒加载
- (VideoView *)videoView
{
    if (_videoView == nil) {
        _videoView = [[VideoView alloc] init];
        
        [self.view addSubview:_videoView];
    }
    return _videoView;
}

- (SliderToolbar *)sliderView
{
    if (_sliderView == nil) {
        _sliderView = [[SliderToolbar alloc] init];
        _sliderView.slider.delegate = self;
        _sliderView.delegate = self;
        [self.view addSubview:_sliderView];
    }
    return _sliderView;
}

- (UIImageView *)backGroundView
{
    if (_backGroundView == nil) {
        _backGroundView = [[UIImageView alloc] initWithFrame:self.view.bounds];
        _backGroundView.image = [UIImage imageNamed:@"shw_unlikeview"];
        [self.view addSubview:_backGroundView];
    }
    return _backGroundView;
}

@end
